package com.gome.ocean.common.utils;

import java.math.BigInteger;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author fuhaining
 */
public class DBSecurityUtil {

    public static String[] encrypt(String plaintext) {
        if (StringUtils.isBlank(plaintext)) {
            throw new IllegalArgumentException("参数plaintext不能为空");
        }

        try {
            String key = RandomStringUtils.randomAlphanumeric(16);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] ciphertextBytes = cipher.doFinal(plaintext.getBytes());
            String ciphertext = new BigInteger(ciphertextBytes).toString(16);

            return new String[] { ciphertext, RandomStringUtils.randomAlphanumeric(2) + key
                                              + RandomStringUtils.randomAlphanumeric(3) };
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }

    public static String decrypt(String ciphertext, String key) {
        if (StringUtils.isBlank(ciphertext)) {
            throw new IllegalArgumentException("参数ciphertext不能为空");
        }
        if (StringUtils.isBlank(key)) {
            throw new IllegalArgumentException("参数key不能为空");
        }
        if (key.length() != 21) {
            throw new IllegalArgumentException("参数错误");
        }

        try {
            key = key.substring(2, 18);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
            byte[] ciphertextBytes = parseHexStr2Byte(ciphertext);
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] plaintextBytes = cipher.doFinal(ciphertextBytes);
            String plaintext = new String(plaintextBytes);

            return plaintext;
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }

    private static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static String dsmDecrypt(String ciphertext, String key) {
        if (StringUtils.isBlank(ciphertext)) {
            throw new IllegalArgumentException("参数ciphertext不能为空");
        }
        if (StringUtils.isBlank(key)) {
            throw new IllegalArgumentException("参数key不能为空");
        }
        if (key.length() != 21) {
            throw new IllegalArgumentException("参数错误");
        }

        try {
            key = key.substring(2, 18);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
            byte[] ciphertextBytes = new BigInteger(ciphertext, 16).toByteArray();
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] plaintextBytes = cipher.doFinal(ciphertextBytes);
            String plaintext = new String(plaintextBytes);

            return plaintext;
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }

    public static void main(String[] args) {
        // String[] result = DBSecurityUtil.encrypt("\"2XA;pox12mOIl\""); //
        // 9Y9K2WE17lPout+X
        // String[] result = DBSecurityUtil.encrypt("Vsd5evjaUzJUHakl");
        // System.out.println(Arrays.asList(result));

        /*
         * String plaintext = DBSecurityUtil.decrypt(
         * "a0f9af79864f2039f824b0fea726e001f5c21d626818a22a79308294b6f532ab",
         * WebSqlContants.GLOBAL_DECRYPT_P); System.out.println(plaintext);
         */
        String abc = DBSecurityUtil.dsmDecrypt("-6b8cb4f6b0c805477d64350ccee3e8b9187376122728d5bd5e8378616db96fe9",
            "s04XkjZcdkW553wahL0J2");
        System.out.println(abc);
        // String plaintext1 =
        // DBSecurityUtil.decrypt("-5687b74b732242e01e47b9014bd145835d42efe96339241803bcecc923d8b785",
        // "s9MYmeCrINOqReWb9s2ht");
        // String plaintext1 =
        // DBSecurityUtil.decrypt("319f8e67f94132266313c9c563463154",
        // "07WUZ9BXimPV8cVQLzaci");

        // -55fbf4eca2dae7388569cefb01b10333549123a7f339f6cbc3f042d96d74fc73
        // O9MQe8blTqcivxZqHGBMR
        // System.out.println(plaintext1); // 6ncmXwco5TLmg
        // tq9tVm93amru2Iiy

    }
}
